package com.ly.lypopupwindowdemo.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.ly.lypopupwindowdemo.MessageCenterBean;
import com.ly.lypopupwindowdemo.OnItemListener;
import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.TXMessageEdit;
import com.ly.lypopupwindowdemo.activity.MessageCenterActivity;
import com.ly.lypopupwindowdemo.adapter.MessageCenterGGAdapter;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Created by liying on 2017/4/20.
 */

public class MessageCenterTXFragment extends Fragment implements TXMessageEdit {

    private MessageCenterGGAdapter adapter;

    private RecyclerView recyclerView;
    private List<MessageCenterBean> lists = new ArrayList<>();

    MessageCenterActivity activity;
    private HashSet<Integer> positionSet;


    private final int CURRENT_SHOW = 1;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case CURRENT_SHOW:
                    if (adapter != null) {
                        adapter.isEdit(false);
                        adapter.notifyDataSetChanged();
                    }

                    break;
            }
        }
    };



    private void initRecycleView() {

        MessageCenterBean bean1 = new MessageCenterBean("前方高能0，亿丰金融福利");
        MessageCenterBean bean2 = new MessageCenterBean("前方高能1，亿丰金融福利");
        MessageCenterBean bean3 = new MessageCenterBean("前方高能2，亿丰金融福利");
        MessageCenterBean bean4 = new MessageCenterBean("前方高能3，亿丰金融福利");
        MessageCenterBean bean5 = new MessageCenterBean("前方高能4，亿丰金融福利");
        MessageCenterBean bean6 = new MessageCenterBean("前方高能5，亿丰金融福利");
        MessageCenterBean bean7 = new MessageCenterBean("前方高能6，亿丰金融福利");
        MessageCenterBean bean8 = new MessageCenterBean("前方高能7，亿丰金融福利");
        MessageCenterBean bean9 = new MessageCenterBean("前方高能8，亿丰金融福利");
        MessageCenterBean bean10 = new MessageCenterBean("前方高能9，亿丰金融福利");
        MessageCenterBean bean11 = new MessageCenterBean("前方高能10，亿丰金融福利");
        MessageCenterBean bean12 = new MessageCenterBean("前方高能11，亿丰金融福利");
        MessageCenterBean bean13 = new MessageCenterBean("前方高能12，亿丰金融福利");
        MessageCenterBean bean14 = new MessageCenterBean("前方高能13，亿丰金融福利");
        MessageCenterBean bean15 = new MessageCenterBean("前方高能14，亿丰金融福利");
        MessageCenterBean bean16 = new MessageCenterBean("前方高能15，亿丰金融福利");

        lists.add(bean1);
        lists.add(bean2);
        lists.add(bean3);
        lists.add(bean4);
        lists.add(bean5);
        lists.add(bean6);
        lists.add(bean7);
        lists.add(bean8);
        lists.add(bean9);
        lists.add(bean10);
        lists.add(bean11);
        lists.add(bean12);
        lists.add(bean13);
        lists.add(bean14);
        lists.add(bean15);
        lists.add(bean16);

        adapter = new MessageCenterGGAdapter(getActivity(), lists, positionSet);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);

        adapter.setOnItemListener(new OnItemListener() {
            @Override
            public void checkBoxClick(int position) {
                //已经有Item被选择，执行添加或删除操作
                addOrRemove(position);

            }

            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(activity, "点击第"+position+"个item", Toast.LENGTH_SHORT).show();

            }
        });

    }

    private void addOrRemove(int position) {

        if (positionSet.contains(position)){
            //如果包含则撤销选择
            Log.e("Message","remove");
            positionSet.remove(position);
        }else{
            //如果不包含，则添加
            Log.e("Message","add");
            positionSet.add(position);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message_center_tx, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycleView);
        positionSet = new HashSet<>();

        initRecycleView();
        activity = (MessageCenterActivity) getActivity();
        activity.setOnTXMessageEdit(this);
        return view;
    }

    @Override
    public void onEdit() {
      //  ToastUtil.showShortToast("提醒onEdit");
        adapter.isEdit(true);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onCancleEdit() {
      //  ToastUtil.showShortToast("提醒onCancles");
        adapter.isEdit(false);
        HashSet<MessageCenterBean> valueSet = new HashSet<>();
        for (Integer integer : positionSet) {
            valueSet.add(adapter.getItem(integer));
        }

        for (MessageCenterBean bean : valueSet) {
            bean.setSelect(false);
        }
        adapter.notifyDataSetChanged();
        positionSet.clear();

    }

    @Override
    public void onCheckAll() {
      //  ToastUtil.showShortToast("提醒onCheckall");
        //adapter.setIsAllCheck(1);
        for (int i=0;i<lists.size();i++){
            lists.get(i).setSelect(true);
            //lists.get(i).isSelect = true;
            positionSet.add(i);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onRead() {
     //   ToastUtil.showShortToast("提醒onRead");
        //判断当前是否全选状态
//        if (adapter!=null){
//            if (adapter.isAllCheck()){
//                //全选
//                adapter.setAllRead(true);
//                adapter.notifyDataSetChanged();
//            }
//        }

        HashSet<MessageCenterBean> valueSet = new HashSet<>();
        for (Integer integer : positionSet) {
            valueSet.add(adapter.getItem(integer));
        }

        for (MessageCenterBean bean : valueSet) {

            bean.setRead(true);
            bean.setSelect(false);
        }
        adapter.notifyDataSetChanged();

    }

    @Override
    public void onDelete() {
       // ToastUtil.showShortToast("提醒onDelete");

        if (positionSet.size()==0){
            //没有选中项
            Toast.makeText(activity, "当前没有选中项哦~", Toast.LENGTH_SHORT).show();
        }
        HashSet<MessageCenterBean>  valueSet = new HashSet<>();
        for (Integer integer : positionSet){
            valueSet.add(adapter.getItem(integer));
        }

        for (MessageCenterBean bean :valueSet){
            adapter.remove(bean);
        }
        adapter.notifyDataSetChanged();
        positionSet.clear();
    }

    @Override
    public void onCancleCheckAll() {
       // ToastUtil.showShortToast("取消提醒全选");

        for (int i=0;i<lists.size();i++){
            lists.get(i).setSelect(false);
            //lists.get(i).isSelect = false;
            positionSet.remove(i);
        }
        adapter.notifyDataSetChanged();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser) {
            Log.e("Message", "提醒页面显示？" + isVisibleToUser);
        } else {
            Log.e("Message", "提醒页面不显示？" + isVisibleToUser);
            handler.sendEmptyMessage(CURRENT_SHOW);
        }
    }


}
